<template>
  <div class="login-page">
    <div class="system-desc">
      <img src="../assets/image/login-logo.png" />
      <div class="desc-text">版权所有 ©2018 上海创信供应链管理有限公司 沪ICP备 18038558号</div>
    </div>
    <div class="right-block">
      <img class="right-bg" src="../assets/image/login-half-circle.png" />
      <div class="form-block">
        <div class="form-block-center">
          <el-form
            label-position="top"
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.native.prevent
            hideRequiredAsterisk
          >
            <el-form-item>
              <div class="title-block">
                <h1>创信医链管理平台</h1>
                <div class="sub-title">CHUANG XIN YI LIAN GUAN LI PING TAI</div>
              </div>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
              <el-input
                v-model.trim="loginForm.username"
                type="text"
                @input="loginForm.username = checkNoChinese(loginForm.username)"
                maxlength="20"
                auto-complete="off"
                placeholder="请输入用户名"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <passport-input v-model="loginForm.password" />
            </el-form-item>
            <el-form-item class="v-code-item" prop="code" label="验证码">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                style="width: 63%"
                maxlength="4"
              >
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" alt="" class="login-code-img" />
              </div>
            </el-form-item>
            <el-form-item style="width: 100%; margin-top: 50px; margin-bottom: 0">
              <el-button
                class="login-btn"
                :loading="loading"
                size="medium"
                type="primary"
                style="width: 100%"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
            <el-form-item style="margin: 0 0 14px 0">
              <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
              <label class="fr forgot-pass" @click="handleForgotPass">忘记密码?</label>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!--忘记密码-->
    <template v-if="dialogVisible">
      <el-dialog :visible.sync="dialogVisible" class="forget-dialog" :close-on-click-modal="false">
        <span slot="title" class="dialog-title">忘记密码</span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="70px"
          class="demo-ruleForm reset-form text-centent"
        >
          <el-form-item label="用户名:" prop="userName">
            <el-input
              placeholder="请输入用户名"
              maxlength="20"
              class="white-bg"
              @input="ruleForm.userName = checkNoChinese(ruleForm.userName)"
              v-model="ruleForm.userName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号:" prop="phoneNum">
            <el-input
              maxlength="11"
              placeholder="请输入手机号"
              @input="ruleForm.phoneNum = checkPhone(ruleForm.phoneNum)"
              v-model="ruleForm.phoneNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="验证码:" class="el-dialog__code" prop="verificationCode">
            <el-input
              maxlength="6"
              v-model="ruleForm.verificationCode"
              @input="ruleForm.verificationCode = checkSpecial(ruleForm.verificationCode)"
              placeholder="请输入验证码"
            ></el-input>
            <el-button
              v-if="!loadingType"
              :loading="phoneLoading"
              size="medium"
              type="text"
              :disabled="single"
              class="fr"
              @click.native.prevent="handleCode"
            >
              <span v-if="!phoneLoading">{{ sendText }}</span>
              <span v-else>发 送 中...</span>
            </el-button>
            <el-button v-else size="medium" class="fr" type="text"
              >{{ countDownNum }} s重新发送
            </el-button>
          </el-form-item>
          <el-form-item class="text-right" label-width="0">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click.native.prevent="handleChangePassFirstSteps()"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog :visible.sync="dialogVisibleSecond" class="forget-second-dialog">
        <span slot="title" class="dialog-title">忘记密码</span>
        <el-form :model="ruleForm" :rules="rules" ref="ruleSecondForm" label-width="96px">
          <el-form-item label="新密码:" prop="newPassword">
            <el-input
              maxlength="20"
              placeholder="请输入8-20位新密码，必须为字母、数字的组合"
              @input="ruleForm.newPassword = checkSpecial(ruleForm.newPassword)"
              type="password"
              v-model="ruleForm.newPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码:" prop="confirmNewPassword">
            <el-input
              maxlength="20"
              placeholder="请确认新密码"
              @input="ruleForm.confirmNewPassword = checkSpecial(ruleForm.confirmNewPassword)"
              type="password"
              v-model="ruleForm.confirmNewPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item class="text-right" label-width="0">
            <el-button @click="closeSecondDialog">取 消</el-button>
            <el-button type="primary" @click.native.prevent="handleChangePass()">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </template>
    <!--重置密码-->
    <template v-if="!forgotFlag">
      <el-dialog
        :visible.sync="dialogRestVisible"
        class="reset-dialog"
        :show-close="false"
        :close-on-click-modal="false"
      >
        <span slot="title" class="dialog-title">修改密码</span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm reset-form text-centent"
        >
          <el-form-item label="用户名:" prop="userName">
            <el-input
              maxlength="20"
              placeholder="请输入用户名"
              :disabled="true"
              class="white-bg"
              v-model="ruleForm.userName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="原密码:" prop="oldPassword">
            <el-input
              placeholder="请输入原始密码"
              maxlength="20"
              @input="ruleForm.oldPassword = checkSpecial(ruleForm.oldPassword)"
              type="password"
              v-model="ruleForm.oldPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码:" prop="newPassword">
            <el-input
              maxlength="20"
              placeholder="请输入8-20位新密码，必须为字母、数字的组合"
              @input="ruleForm.newPassword = checkSpecial(ruleForm.newPassword)"
              type="password"
              v-model="ruleForm.newPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码:" prop="confirmNewPassword">
            <el-input
              maxlength="20"
              placeholder="请确认新密码"
              @input="ruleForm.confirmNewPassword = checkSpecial(ruleForm.confirmNewPassword)"
              type="password"
              v-model="ruleForm.confirmNewPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号:" prop="phoneNum">
            <el-input
              maxlength="11"
              placeholder="请输入手机号"
              :disabled="true"
              class="white-bg"
              @input="ruleForm.phoneNum = checkPhone(ruleForm.phoneNum)"
              v-model="ruleForm.phoneNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="验证码:" prop="verificationCode" class="el-dialog__code">
            <el-input
              maxlength="6"
              v-model="ruleForm.verificationCode"
              @input="ruleForm.verificationCode = checkSpecial(ruleForm.verificationCode)"
              placeholder="请输入验证码"
            ></el-input>
            <el-button
              v-if="!loadingType"
              :loading="phoneLoading"
              size="medium"
              type="text"
              :disabled="single"
              class="fr"
              @click.native.prevent="handleCode"
            >
              <span v-if="!phoneLoading">{{ sendText }}</span>
              <span v-else>发 送 中...</span>
            </el-button>
            <el-button v-else size="medium" class="fr" type="text"
              >{{ countDownNum }} s重新发送
            </el-button>
          </el-form-item>
          <el-form-item class="text-right">
            <el-button type="primary" @click.native.prevent="handleRestPass">确认提交</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import {mobileRegRule} from "@/utils/rule";
import {checkPhone, checkSpecial, checkSpace, checkNoChinese} from "@/utils/index";
import {
  getCodeImg,
  getUserInfo,
  getChangUserPassword,
  forgetPassword,
  forgetPasswordCheckUser,
  forgetPasswordCheckUserSendCode,
} from "@/api/login";
import Cookies from "js-cookie";
import {removeToken} from "@/utils/auth";
import {pwdLength} from "@/utils/rule";
import {encrypt, decrypt} from "@/utils/jsencrypt";
import {activeOnLoad, encrypSignMsgPKCS7} from "@/utils/chuangxin";
import {Message, MessageBox} from "element-ui";
import {mapMutations} from "vuex";
import PassportInput from "@/views/components/PassportInput";

export default {
  name: "Login",
  components: {PassportInput},
  data() {
    // 密码校验
    let passwordTest = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
    // let passwordTest = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
    // let passwordTest = /^(?![\d]+$)(?![a-zA-Z]+$)(?![!@#$%^&*_~+?><-|]+$)[\da-zA-Z!@#$%^&*_~+?><-|]{6,20}$/;
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value.length < 8 || value.length > 20) {
        callback(new Error("密码必须包含8-20位字母数字组合，请重新设置"));
      } else if (value.indexOf(" ") >= 0) {
        callback(new Error("密码中不能含有空格"));
      } else if (this.ruleForm.oldPassword == value) {
        callback(new Error("新密码不能与旧密码相同，请重新输入"));
      } else if (!passwordTest.test(value)) {
        callback(new Error("密码必须包含8-20位字母数字组合，请重新设置"));
      } else {
        if (this.ruleForm.confirmNewPassword !== "") {
          this.$refs.ruleForm.validateField("confirmNewPassword");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入确认密码"));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error("您输入的密码不一致，请重新输入"));
      } else {
        callback();
      }
    };
    // 手机校验
    let phoneValidate = (rule, value, callback) => {
      if (value === "") {
        this.single = true;
        callback(new Error("请输入手机号"));
      } else if (!mobileRegRule.test(value)) {
        this.single = true;
        callback(new Error("请输入正确的手机号码"));
      } else {
        this.single = false;
        callback();
      }
    };
    return {
      bSelectCertResult: false,
      userInfoData: {}, // 用户的状态
      CryptoAgent: "",
      codeUrl: "",
      cookiePassword: "",
      dialogRestVisible: true, // 判断是否需要重置密码
      dialogVisible: false, // 判断是否需要修改密码
      dialogVisibleSecond: false, // 判断修改密码二次弹窗
      loadingType: false, // 是否发送
      sendText: "发送验证码", // 发送验证码文案
      codeTimer: null,
      timer: null,
      countDownNum: 60, // 60s重新发送
      // single: true,
      forgotFlag: true, // 区分忘记密码和第一次强制修改密码的操作
      ruleForm: {
        confirmNewPassword: "",
        newPassword: "",
        oldPassword: "",
        phoneNum: "",
        userName: "",
        verificationCode: "",
      },
      loginForm: {
        sourceData: "", // 加签前源数据
        signData: "", // 加签后加签数据
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [{required: true, trigger: "blur", message: "请输入用户名"}],
        password: [
          {required: true, trigger: "blur", message: "请输入密码"},
          {
            pattern: pwdLength,
            message: "密码为8~20位数字、字母组成",
            trigger: "blur",
          },
        ],
        code: [{required: true, trigger: "blur", message: "请输入验证码"}],
      },
      rules: {
        oldPassword: [
          {
            pattern: passwordTest,
            message: "密码必须包含8-20位字母数字组合，请重新设置",
            trigger: "blur",
          },
          {
            required: true,
            trigger: "blur",
            message: "原密码不能为空",
          },
        ],
        newPassword: [
          {required: true, validator: validatePass, trigger: "blur"},
          {
            min: 8,
            max: 20,
            message: "密码必须包含8-20位字母数字组合，请重新设置",
            trigger: "blur",
          },
        ],
        confirmNewPassword: [
          {required: true, validator: validatePass2, trigger: "blur"},
          {
            min: 8,
            max: 20,
            message: "密码必须包含8-20位字母数字组合，请重新设置",
            trigger: "blur",
          },
        ],
        userName: [
          {
            required: true,
            trigger: "blur",
            message: "用户名不能为空",
          },
        ],
        phoneNum: [{required: true, trigger: "blur", validator: phoneValidate}],
        verificationCode: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      loading: false,
      phoneLoading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  computed: {
    // 发送验证码是否置灰
    single: {
      get: function () {
        let isFlag = true;
        isFlag = !mobileRegRule.test(this.ruleForm.phoneNum);
        return isFlag;
      },
      set: function () {
        return true;
      },
    },
    isDept: function () {
      return (
        this.userInfoData.deptType == "100" ||
        this.userInfoData.deptType == "101" ||
        this.userInfoData.deptType == "102" ||
        this.userInfoData.deptType == "103"
      );
    },
    isSupplier: function () {
      return this.userInfoData.deptType == "102" || this.userInfoData.deptType == "103";
    },
    isCore: function () {
      return this.userInfoData.deptType == "100" || this.userInfoData.deptType == "101";
    },
  },
  created() {
    this.CryptoAgent = activeOnLoad();
    this.getCode();
    this.getCookie();
    this.clearSession();
  },
  methods: {
    ...mapMutations(["SET_DEPTYPE", "SET_TICK_CFCA_AGREEMENT"]),
    checkPhone,
    checkSpecial,
    checkSpace,
    checkNoChinese,
    // 忘记密码
    handleForgotPass() {
      this.single = true;
      this.loadingType = false;
      this.sendText = "发送验证码";
      this.forgotFlag = true;
      this.dialogVisible = true;
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.loading = false;
      this.single = true;
      this.loadingType = false;
      this.sendText = "发送验证码";
      this.ruleForm.oldPassword = "";
      this.ruleForm.newPassword = "";
      this.ruleForm.confirmNewPassword = "";
      this.ruleForm.verificationCode = "";
      if (this.forgotFlag) {
        this.ruleForm.phoneNum = "";
        this.ruleForm.userName = "";
      }
      this.getCode();
    },
    // 关闭二次忘记密码弹窗
    closeSecondDialog() {
      this.dialogVisibleSecond = false;
      this.ruleForm.newPassword = "";
      this.ruleForm.confirmNewPassword = "";
    },
    clearLoginForm() {
      this.$set(this.loginForm, "password", "");
      this.$set(this.loginForm, "code", "");
      // this.loginForm.password = '';
      // this.loginForm.code = '';
    },
    // 修改用户密码第一步-校验用户信息
    handleChangePassFirstSteps() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          forgetPasswordCheckUser(this.ruleForm)
            .then(res => {
              if (res.code == 200) {
                this.dialogVisibleSecond = true;
              } else {
                this.ruleForm.verificationCode = "";
                let msg = res.msg || "用户信息有误";
                Message.error(msg);
              }
            })
            .catch(err => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 修改用户密码第二步-忘记密码
    handleChangePass() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const gloLoading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          forgetPassword(this.ruleForm)
            .then(res => {
              if (res.code === 200) {
                this.$alert("修改成功，请重新登录", "提示", {
                  confirmButtonText: "确定",
                  type: "success",
                  callback: action => {
                    this.dialogVisibleSecond = false;
                    this.dialogVisible = false;
                    this.loading = false;
                    this.ruleForm.oldPassword = "";
                    this.ruleForm.newPassword = "";
                    this.ruleForm.confirmNewPassword = "";
                    this.ruleForm.verificationCode = "";
                    if (this.forgotFlag) {
                      this.ruleForm.phoneNum = "";
                      this.ruleForm.userName = "";
                    }
                    this.clearLoginForm();
                    this.getCode();
                  },
                });
              } else {
                Message.error(res.msg || "修改失败");
              }
              gloLoading.close();
            })
            .catch(() => {
              gloLoading.close();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 第一次登录强制修改密码
    handleRestPass() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const gloLoading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          getChangUserPassword(this.ruleForm)
            .then(res => {
              if (res.code === 200) {
                this.$alert("修改成功，请重新登录", "提示", {
                  confirmButtonText: "确定",
                  type: "success",
                  callback: action => {
                    this.dialogRestVisible = false;
                    this.loading = false;
                    // 20201016
                    this.loadingType = false;
                    this.sendText = "发送验证码";
                    this.ruleForm.oldPassword = "";
                    this.ruleForm.newPassword = "";
                    this.ruleForm.confirmNewPassword = "";
                    this.ruleForm.verificationCode = "";
                    if (this.forgotFlag) {
                      this.ruleForm.phoneNum = "";
                      this.ruleForm.userName = "";
                    }
                    this.clearLoginForm();
                    this.getCode();
                  },
                });
              } else {
                this.ruleForm.verificationCode = "";
                Message.error(res.msg || "修改失败");
              }
              gloLoading.close();
            })
            .catch(() => {
              gloLoading.close();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取手机验证码
    handleCode() {
      this.phoneLoading = true;
      // let option = {
      //     phoneNumber: this.ruleForm.phoneNum,
      //     userName: this.ruleForm.userName
      // }
      forgetPasswordCheckUserSendCode(this.ruleForm)
        .then(res => {
          if (res.code === 200) {
            Message({
              message: "短信发送成功",
              type: "success",
            });
            this.phoneLoading = false;
            this.loadingType = true;
            this.countDown();
          } else {
            Message({
              message: res.msg,
              type: "error",
            });
            this.ruleForm.phoneNum = "";
            this.ruleForm.userName = "";
            this.single = true;
            this.phoneLoading = false;
            this.loadingType = false;
          }
        })
        .catch(err => {
          this.phoneLoading = false;
          this.loadingType = false;
        });
    },
    // 60S倒计时
    countDown() {
      clearTimeout(this.timer);
      this.countDownNum = 60;
      this.timer = setInterval(() => {
        this.countDownNum--;
        if (this.countDownNum <= 0) {
          clearInterval(this.timer);
          this.loadingType = false;
          this.countDownNum = 60;
          this.sendText = "重新发送";
        }
      }, 1000);
    },
    // 定时获取1分55秒获取验证码
    getCodeSetTime() {
      this.codeTimer = setInterval(this.getCodeImgFun, 115000);
    },
    // 点击获取验证码图片
    getCode() {
      if (this.codeTimer) {
        clearInterval(this.codeTimer);
      }
      this.getCodeImgFun();
      this.getCodeSetTime();
    },
    // 获取验证码图片
    getCodeImgFun() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
        this.$set(this.loginForm, "code", "");
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.queryUserInfo(); // 获取用户信息
        }
      });
    },
    // 获取用户信息
    queryUserInfo() {
      getUserInfo(this.loginForm.username)
        .then(res => {
          if (res && res.code === 200) {
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {
                expires: 1,
              });
              Cookies.set("password", encrypt(this.loginForm.password), {
                expires: 1,
              });
              Cookies.set("rememberMe", this.loginForm.rememberMe, {
                expires: 1,
              });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove("rememberMe");
            }
            // if(!res.data) return;
            let tickCfcaAgreement = res.data.tickCfcaAgreement || "1";
            this.$store.commit("SET_TICK_CFCA_AGREEMENT", tickCfcaAgreement);
            this.userInfoData = res.data || {};
            // alert(this.userInfoData.dept.registerActivity);
            this.roles = this.userInfoData.roleKeys.split(",");
            // if(this.userInfoData.isRealAuth == 4 && this.roles.indexOf("supplier_reviewer")<0) { //证书已经下载
            if (this.userInfoData.isRealAuth == 4 || this.userInfoData.isRealAuth == 5) {
              //证书已经下载
              // 消息加签
              this.encrypSignMsg();
            } else {
              //正常登录
              this.queryLogin();
            }
          } else {
            Message.error(res.msg || "登录失败");
            this.loading = false;
            this.getCode();
          }
        })
        .catch(err => {
          this.loading = false;
          this.getCode();
        });
    },
    // 请求登录接口
    queryLogin() {
      this.$store
        .dispatch("Login", this.loginForm)
        .then(() => {
          // res.data.isRealAuth  0:未认证 1：个人已认证(人脸识别)，2：企业已认证，3:已申请证书 4:已下载证书 5 完成平台注册协议签署
          localStorage.setItem("realName", "");
          localStorage.setItem("idNumber", "");
          if (this.userInfoData.isFirstLogin) {
            //第一次登录，重置密码
            this.$store.commit("SET_TOKEN", "");
            this.$store.commit("SET_ROLES", []);
            this.$store.commit("SET_PERMISSIONS", []);
            removeToken();
            this.forgotFlag = false;
            // 20201016
            this.loadingType = false;
            this.sendText = "发送验证码";
            // this.phoneLoading = false;
            this.ruleForm.userName = this.userInfoData.userName;
            this.ruleForm.phoneNum = this.userInfoData.phonenumber;
            this.single = false;
            // this.dialogVisible = true;
            this.dialogRestVisible = true;
          } else if (this.userInfoData.isRealAuth == 0 && this.isDept) {
            //未完成人脸识别
            this.gotoFaceIndentify();
          } else if (
            this.userInfoData.isRealAuth == 1 &&
            this.isSupplier &&
            this.userInfoData.registerActivity == 9
          ) {
            //registerActivity 为9时供应商企业信息已审核
            //供应商未完成企业认证
            this.gotoEnterpriseIndentify();
          } else if (this.userInfoData.isRealAuth == 3 && this.isDept) {
            //证书未下载
            this.$router.push({
              path: "/user/verificate/operatorsVerificate",
            });
          } else {
            this.$router.push({path: "/"});
          }
        })
        .catch(() => {
          this.loading = false;
          this.getCode();
        });
    },
    // 消息加签
    encrypSignMsg() {
      // 消息加签
      let sourceArr = [];
      sourceArr.push(this.loginForm.username);
      sourceArr.push(new Date().getTime());
      console.log("加签前", sourceArr);
      // sourceArr.push(this.loginForm.password)
      encrypSignMsgPKCS7(this.CryptoAgent, sourceArr)
        .then(encrypData => {
          this.loginForm.sourceData = sourceArr.join(",");
          this.loginForm.signData = encrypData;
          console.log("加签");
          console.log("encrypData", encrypData);
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              console.log("登录跳转");
              if (
                this.userInfoData.isRealAuth == 4 &&
                this.roles.indexOf("supplier_reviewer") >= 0
              ) {
                this.$alert("请完成平台注册协议签署", "提示", {
                  confirmButtonText: "签 署",
                  showClose: false,
                  type: "warning",
                }).then(() => {
                  this.$router.push({
                    path: "/contractComfirmDetails",
                    query: {
                      num: "",
                      type: "register",
                      templateIds: "H101,H102",
                    },
                  });
                });
              } else {
                this.$router.push({path: "/"});
              }
            })
            .catch(() => {
              this.loginForm.sourceData = "";
              this.loginForm.signData = "";
              this.$store.commit("SET_TOKEN", "");
              this.$store.commit("SET_ROLES", []);
              this.$store.commit("SET_PERMISSIONS", []);
              removeToken();
              this.$store.commit("SER_REGGISTERCOMPANYNAME", this.userInfoData.deptName);
              this.$store.commit("SER_REGGISTERUSERNAME", this.loginForm.username);
              this.$store.commit("SET_REGISTERPHONENUMBER", this.userInfoData.phonenumber);
              this.$store.commit("SET_DEPTYPE", this.userInfoData.deptType);
              this.loading = false;
              this.getCode();
            });
        })
        .catch(err => {
          this.loading = false;
          this.getCode();
        });
    },
    // 删除上一次注册的缓存信息
    clearSession() {
      this.$store.commit("SET_TOKEN", "");
      this.$store.commit("SET_ROLES", []);
      this.$store.commit("SET_PERMISSIONS", []);
      removeToken();
      this.$store.commit("SET_REGGISTERTPE", 0);
      this.$store.commit("SER_REGGISTERUSERNAME", "");
      this.$store.commit("SER_REGGISTERCOMPANYNAME", "");
      this.$store.commit("SET_REGISTERREALNAME", "");
      this.$store.commit("SET_REGISTERPHONENUMBER", "");
      this.$store.commit("SET_USERSTATUS", "");
      sessionStorage.removeItem("registerType");
      sessionStorage.removeItem("registerUserName");
      sessionStorage.removeItem("registerCompanyName");
      sessionStorage.removeItem("registerRealName");
      sessionStorage.removeItem("registerPhonenumber");
      sessionStorage.removeItem("userStatus");
    },
    // 去人脸识别
    gotoFaceIndentify() {
      this.$alert("您尚未进行实名认证，即将为您开启", {
        showConfirmButton: false,
        showClose: false,
        type: "warning",
        customClass: "simple-message-box",
      });
      let that = this;
      window.setTimeout(function () {
        MessageBox.close();
        that.$router.push({path: "/recognition"});
      }, 2500);
    },
    // 去企业认证
    gotoEnterpriseIndentify() {
      // if(!(this.roles.indexOf("supplier_operator")>=0||this.roles.indexOf("supplier_reviewer")>=0)){//非供应商时
      //     return false;
      // }
      let that = this;
      this.$alert("您尚未进行企业实名认证，即将为您开启", {
        showConfirmButton: false,
        showClose: false,
        type: "warning",
        customClass: "simple-message-box",
      });
      let routerUrl = "";
      if (this.roles.indexOf("supplier_operator") >= 0) {
        //供应商操作员
        routerUrl = "/user/verificate/enterpriseVerificate";
      } else if (this.roles.indexOf("supplier_reviewer") >= 0) {
        //供应商复核员
        routerUrl = "/user/verificate/publicFunds";
      }
      window.setTimeout(function () {
        MessageBox.close();
        that.$router.push({path: routerUrl});
      }, 2500);
    },
    // 去签署平台注册协议
    gotoMarkRegistPage() {
      if (this.roles.indexOf("supplier_reviewer") >= 0) {
        this.$alert("请完成平台注册协议签署", "提示", {
          confirmButtonText: "签 署",
          showClose: false,
          type: "warning",
        }).then(() => {
          this.$router.push({
            path: "/contractComfirmDetails",
            query: {
              num: "",
              type: "register",
              templateIds: "H101,H102",
            },
          });
        });
      }
    },
  },
  beforeDestroy() {
    // 如果定时器还在运行 或者直接关闭，不用判断
    if (this.timer) {
      clearInterval(this.timer);
    }
    if (this.codeTimer) {
      clearInterval(this.codeTimer);
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
$input-height: 56px;
.reset-dialog,
.forget-dialog {
  .dialog-title {
    font-size: 16px;
    font-weight: bold;
  }
  .reset-form {
    .white-bg {
      input {
        background-color: #ffffff;
        color: #606266;
      }
    }
  }
}

.login-form {
  padding: 38px 59px 0 59px;
  .el-input {
    input {
      height: $input-height;
    }
  }
  .input-icon {
    height: 100%;
    width: 14px;
    margin-left: 2px;
  }
  .login-btn {
    font-size: 16px;
    padding: 0 !important;
    line-height: $input-height;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: $input-height;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 100%;
}
</style>
<style rel="stylesheet/scss" lang="scss">
.reset-dialog .el-dialog {
  width: 480px;
  height: 578px;
}
</style>
<!--新登录页样式-->
<style lang="scss">
@import "./login.scss";
</style>
